package persistency;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import model.Customer;
import model.Operator;
import model.ServiceCall;

public class OperatorRepository  implements Repository<Operator> {

	private static final String PERSISTENCE_UNIT_NAME = "MiniServiceDesk";
	
	@Override
	public void insert(Operator entity) throws PersistencyException {
		  EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager(); 
		  EntityTransaction tx = em.getTransaction(); 
		  try
		  {
			  tx.begin(); 
			  em.persist(entity); 
			  em.flush();
			  tx.commit(); 
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }			
	}

	@Override
	public void update(Operator entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			em.merge(entity);
			tx.commit();
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }
	}

	@Override
	public void delete(Operator entity) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			Operator dObject = getById(entity.getOperatorId());
			em.remove(dObject);
			tx.commit();
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }
	}

	@Override
	public List<Operator> getAll() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			List<Operator> dObjects = em.createQuery("Select operator from Operator operator").getResultList();			
			tx.commit();
			return dObjects;
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }
	}
	
	@Override
	public Operator getById(Object id) throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			Operator gObject = em.find(Operator.class, id);
			tx.commit();
			return gObject;
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }
	}

	@Override
	public int getCount() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			int pCount = Integer.parseInt(em
					.createQuery("Select count(operator.operatorId) from Operator operator")
					.getSingleResult().toString());
			tx.commit();
			return pCount;
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }
	}
	
	@SuppressWarnings("unchecked")
	public List<Customer> getAllActive() throws PersistencyException {
		EntityManager em = Persistence.createEntityManagerFactory(
				PERSISTENCE_UNIT_NAME).createEntityManager();
		EntityTransaction tx = em.getTransaction();
		try {
			tx.begin();
			List<Customer> dObjects = em.createQuery(
					"Select operator from Operator operator where active = true")
					.getResultList();
			tx.commit();
			return dObjects;
		  } catch (Exception ex) {
				if (tx != null && tx.isActive()) {
					tx.rollback();
				}
				throw new PersistencyException("Could not perform action.Rollback triggered!", ex);
		  } finally {
				if (em != null && em.isOpen()) {
					em.close();
				}
		  }
	}

}